﻿var personStatesStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: '../Service/PersonService.aspx?Action=GetStates'
    }),
    reader: new Ext.data.JsonReader({
        root: 'topics',
        totalProperty: 'totalCount',
        id: 'Name',
        fields: ['Name', 'Id', 'Code']
    }),
    remoteStore: true
});
personStatesStore.load();
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');

FantastPlatform.UI.AddPersonForm = function (config) {
    Ext.override(Ext.form.CheckboxGroup, {
        //在inputValue中找到定义的内容后，设置到items里的各个checkbox中    
        setValue: function (value) {
            this.items.each(function (f) {
                if (value.indexOf(f.inputValue) != -1) {
                    f.setValue(true);
                } else {
                    f.setValue(false);
                }

            });
        },
        //以value1,value2的形式拼接group内的值   
        getValue: function () {
            var re = "";
            this.items.each(function (f) {
                if (f.getValue() == true) {
                    re += f.inputValue + ",";
                }
            });
            return re.substr(0, re.length - 1);
        },
        //在Field类中定义的getName方法不符合CheckBoxGroup中默认的定义，因此需要重写该方法使其可以被BasicForm找到   
        getName: function () {
            return this.name;
        }
    });
    Ext.apply(this, config);
    var cl = this.onSClick;
    var addPersonForm = this;
    var parentForm = this.ParentFrom;

    function getFormValue(form) {
        if (form.form.isValid()) {
            var dataStr = Ext.util.JSON.encode(form.form.getValues());
            return dataStr;
        }
        return '';
    }
    function setFormValue() {

    }
    var personTypeStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../Service/PersonService.aspx?Action=GetPersonType'
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Name',
            fields: ['Name', 'Id', 'Code']
        }),
        remoteStore: true
    });
    var personStatesStoreCheckboxItems = [];
    var count = personStatesStore.getCount();
    for (var i = 0; i < count; i++) {
        var boxLabel = personStatesStore.getAt(i).get("Name");
        var name = personStatesStore.getAt(i).get("Id");
        var value = personStatesStore.getAt(i).get("Id");
        personStatesStoreCheckboxItems.push({
            boxLabel: boxLabel,
            name: 'personStates' + name,
            value: value,
            inputValue: name,
            width: 100
        });
    }
    var personStatesCheckboxGroup = new Ext.form.CheckboxGroup({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;人员状态',
        name: 'PersonStates',
        hiddenName: 'PersonStates',
        displayField: 'Name',
        mode: 'remote',
        valueField: 'Id',
        allowBlank: false,
        editable: false,
        triggerAction: 'all',
        blankText: '请选择人员状态',
        columns: 2,
        items: personStatesStoreCheckboxItems

    });
    var personRelationTypeStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../Service/PersonService.aspx?Action=GetPersonRelationType'
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Name',
            fields: ['Name', 'Id', 'Code']
        }),
        remoteStore: true
    });
    personTypeStore.load();
    personRelationTypeStore.load();
    var sexStore = new Ext.data.SimpleStore({
        fields: ['Name', 'Value'],
        data: [['男', '男'], ['女', '女']]
    });
    //    var typeStore = new Ext.data.SimpleStore({
    //        fields: ['Name', 'Value'],
    //        data: [['所有者', '所有者'], ['承租人', '承租人'], ['普通人员', '普通人员']]
    //    });
    var sexComboBox = new Ext.form.ComboBox({
        fieldLabel: '性别',
        name: 'Sex',
        hiddenName: 'Sex',
        store: sexStore,
        displayField: 'Name',
        mode: 'local',
        valueField: 'Value',
        allowBlank: true,
        editable: false,
        triggerAction: 'all',
        emptyText: '选择性别',
        readOnly: true,
        anchor: '90%'
    });
    var typeComboBox = new Ext.form.ComboBox({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;人员类型',
        name: 'PersonType',
        hiddenName: 'PersonType',
        store: personTypeStore,
        displayField: 'Name',
        mode: 'local',
        valueField: 'Id',
        allowBlank: false,
        editable: false,
        triggerAction: 'all',
        emptyText: '选择人员类别',
        readOnly: true,
        anchor: '90%'
    });
    var relationTypeComboBox = new Ext.form.ComboBox({
        fieldLabel: '与房主关系',
        name: 'PersonRelationType',
        hiddenName: 'PersonRelationType',
        store: personRelationTypeStore,
        displayField: 'Name',
        mode: 'local',
        valueField: 'Id',
        allowBlank: true,
        editable: false,
        triggerAction: 'all',
        emptyText: '选择人员与房主的关系',
        readOnly: true,
        anchor: '90%'
    });
    this.items = [{
        layout: 'column',
        border: false,
        labelSeparator: '：',
        items: [
        {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;姓名',
                name: 'Name',
                allowBlank: false,
                blankText: "姓名不能为空",
                anchor: '90%'
            })]
        }, {
            border: false,
            layout: 'form',
            items: [new Ext.form.Hidden({
                name: 'CreateDate'
            })]
        }, {
            border: false,
            layout: 'form',
            items: [new Ext.form.Hidden({
                name: 'Enable'
            })]
        }, {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [sexComboBox]
        }, {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: 'QQ',
                name: 'QQ',
                anchor: '90%'
            })]
        },
        {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '电话',
                name: 'Tel',
                anchor: '90%'
            })]
        },
        {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '邮箱',
                name: 'Email',
                anchor: '90%'
            })]
        }, {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '手机号码',
                name: 'Phone',
                anchor: '90%'
            })]
        },
        {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '民族',
                name: 'Nation',
                anchor: '90%'
            })]
        }, {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [relationTypeComboBox]
        }, {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '籍贯',
                name: 'NativePlace',
                anchor: '90%'
            })]
        }, {
            columnWidth: 0.5,
            border: false,
            layout: 'form',
            items: [typeComboBox]
        }, {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;身份证号码',
                name: 'IdCard',
                maxLength: 18,
                minLength: 15,
                regex: /^(\d{18,18}|\d{15,15}|\d{17,17}x|\d{17,17}X)$/,
                regexText: '输入正确的身份号码',
                anchor: '95%'
            })]
        }
        , {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '现居住地址',
                name: 'Address',
                anchor: '95%'
            })]
        }
         , {
             columnWidth: 1,
             border: false,
             layout: 'form',
             items: [new Ext.form.TextField({
                 fieldLabel: '户籍地址',
                 name: 'RegisterAddress',
                 anchor: '95%'
             })]
         }, {
             columnWidth: 1,
             border: false,
             layout: 'form',
             items: [personStatesCheckboxGroup]
         }
        , {
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.TextField({
                fieldLabel: '描述',
                name: 'Description',
                anchor: '95%',
                height: 50
            })]
        }]
    }];

    FantastPlatform.UI.AddPersonForm.superclass.constructor.call(this, {
        bodyStyle: 'padding:5px',
        collapsible: true,
        labelAlign: 'right',
        autoScroll: true,
        border: false
    });
};

Ext.extend(FantastPlatform.UI.AddPersonForm, Ext.form.FormPanel, {
      onRender: function (ct, position) {
        FantastPlatform.UI.AddPersonForm.superclass.onRender.call(this, ct, position);
        this.add(new Ext.form.Hidden({ name: 'HouseId', value: this.HouseId }));
        if (this.Id && this.Id != 0) {
            this.add(new Ext.form.Hidden({ name: 'Id', value: this.Id }));
            var AddPersonForm = this.form
            var personId = this.Id;
            var personType = this.PersonType
            AddPersonForm.load({
                url: '../Service/PersonService.aspx?Action=Get',
                params: { id: personId, type: personType },
                waitMsg: '正在读取数据...',
                waitTitle: '请稍后...',
                failure: function (form, action) {
                    if (action.failureType != 'client') {
                        showErrorMessage('操作失败', action.result.message, '');
                    }

                }
            });
            Ext.Ajax.request({
                method: 'post',
                url: "../Service/PersonService.aspx?Action=GetStatesByPersonId",
                params: { id: personId },
                success: function (request, options) {
                    // 服务器返回json形式的结果                                          
                    var reText = request.responseText;
                    var reTextObj = Ext.util.JSON.decode(reText);
                    if (eval(reTextObj.success)) {
                        AddPersonForm.findField('PersonStates').setValue(reTextObj.message);
                    }
                }
            });
        }
    }
});
